<template>
  <view v-if="visible">
    <view class="overlay" @click="closeDialog"></view>
    <view class="dialog">
      <view class="content">
        {{messageTxt}}
        <slot></slot>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    messageTxt: {
      type: String,
      default: ''
    },
    confirmModalTxt: {
      type: String,
      default: '用于获取设备权限进行二维码、条形码扫描和图片识别,请打开手机相机、相册（存储）权限（点击确定后在权限中授权相机相册功能）'
    },
    type: {
      type: String,
      default: '',
    }
  },
  created () {
    this.permission()
  },
  methods: {
    permission () {
      let compat = plus.android.importClass('androidx.core.content.ContextCompat')
      let context = plus.android.runtimeMainActivity() //上下文
      let permission_CAMERA = 'android.permission.CAMERA'
      let permission_READ_MEDIA_IMAGES = 'android.permission.READ_MEDIA_IMAGES'
      let permission_READ_EXTERNAL_STORAGE = 'android.permission.READ_EXTERNAL_STORAGE'
      let CAMERA = compat.checkSelfPermission(context, permission_CAMERA)
      let READ_MEDIA_IMAGES = compat.checkSelfPermission(context, permission_READ_MEDIA_IMAGES)
      let READ_EXTERNAL_STORAGE = compat.checkSelfPermission(context, permission_READ_EXTERNAL_STORAGE)
      // 返回0则表示已授权该权限， 为 - 1 则未授权
      // console.log(CAMERA, READ_MEDIA_IMAGES, READ_EXTERNAL_STORAGE)
      if (!CAMERA && !READ_MEDIA_IMAGES && !READ_EXTERNAL_STORAGE) {
        this.$emit('updateVisible', false);
        this.$emit('scanCoding', this.type === 'im' ? true : false);
      }

      if (CAMERA) {
        plus.android.requestPermissions(['android.permission.CAMERA'],
          (e) => {
            if (e.granted.length) {
              // console.log('允许相机', e)
              // this.$emit('updateVisible', false);
              // this.$emit('scanCoding');
              if (READ_MEDIA_IMAGES) {
                plus.android.requestPermissions(['android.permission.READ_EXTERNAL_STORAGE',
                  'android.permission.READ_MEDIA_IMAGES'
                ],
                  (res) => {
                    if (res.granted.length) {
                      // console.log('允许存储', res)
                      this.$emit('updateVisible', false);
                      this.$emit('scanCoding', this.type === 'im' ? true : false);
                    } else {
                      // console.log('拒绝', e)
                      this.$emit('updateVisible', false);
                      this.confirmModal('', this.confirmModalTxt)
                    }
                  })
              }
            } else {
              // console.log('拒绝', e)
              this.$emit('updateVisible', false);
              this.confirmModal('', this.confirmModalTxt)
            }
          })
      }
    },
    confirmModal (title, content) {
      uni.showModal({
        title: title || '提示',
        content: content,
        // showCancel: false, // 不显示取消按钮
        success (res) {
          if (res.confirm) {
            var Intent = plus.android.importClass("android.content.Intent");
            var Settings = plus.android.importClass(
              "android.provider.Settings");
            var Uri = plus.android.importClass("android.net.Uri");
            var mainActivity = plus.android.runtimeMainActivity();
            var intent = new Intent();
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            var uri = Uri.fromParts("package", mainActivity.getPackageName(),
              null);
            intent.setData(uri);
            mainActivity.startActivity(intent);
          }
        }
      });
    },
    // 关闭弹框方法
    closeDialog () {
      this.$emit('updateVisible', false);
    },
  },
};
</script>

<style>
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 999;
}

.dialog {
  position: fixed;
  top: 30%;
  left: 50%;
  width: 60%;
  transform: translate(-50%, -50%);
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  z-index: 1000;
}

.content {
  /* Add styling for the content if needed */
}
</style>